﻿@using SqlSugar.Extensions;
@using Generate.Service
<template>
    <vxe-modal ref="modal" v-model="modalShow" v-bind="modalOption" @@show="open" @@confirm="confirm" @@close="close" :before-hide-method="beforeHideMethod">
        <vxe-form ref="form" v-bind="formOption"></vxe-form>
    </vxe-modal>
</template>

<script>
export default {
    props: ['show'],
    props: { data: { type: Object, default: {} } },
    data() {
        return {
            modalShow: this.show,
            modalOption: { title: '', type: 'confirm', showFooter: true, width: window.innerWidth * 0.8, height: window.innerHeight * 0.6, confirmButtonText: '保存' },
            formOption: {
				data: {},
				titleWidth: 100,
				titleAlign: 'right',
				titleOverflow: true,
				preventSubmit: true,
				className: 'form-content',
                items: [
                    { field: 'Id', title: '主键', visible: false, resetValue: '', itemRender: { name: '$text' }  },
                    {
                        field: 'Pid',
                        title: '上级',
                        resetValue: '',
                        span: 24,
                        itemRender: { name: '$treeSelect', props: { data: [], checkStrictly: true, placeholder: '选择上级' } },
                    },
                    @foreach (var t in @Model.Columns)
                    {
                        @:{ field: '@t.Name', title: '@t.Description.Replace("'","\"")', resetValue: @(TemplateService.GetFormVueDefultValue(t)), span: 24, itemRender: { name: '$input' } },
                    }
                ],
                rules: {
                    @foreach (var t in @Model.Columns)
                    {
                        if (!t.IsNullable)
                        {
                        @:@t.Name: [{ required: true, message: @("'请输入" + t.Description.Replace("'", "\"")+"'")}],
                        }
                    }
                }
            }
        }
    },
    watch: {
        modalShow: {
            immediate: true,
            handler: function (val) {
                this.$emit('update:show', val);
            }
        },
        data: {
            immediate: true,
            deep: true,
            handler: function (val) {
                this.formOption.data = val || {};
                this.modalOption.title = `${IsEmpty(val) ? '新增' : '编辑'}`;
            }
        }
    },
    methods: {
        async open() {
            this.$post(this.serverApi.@(Model.EntityName).list, { isOption: true }).then((res) => {
                //父级筛选项
                this.formOption.items.find((t) => t.field == 'Pid').itemRender.props.data = res.data;
                if (IsEmpty(this.data)) this.$refs.form.reset();
            });
        },
        async confirm() {
            let err = await this.$refs.form.validate().catch(err => err);
            if (IsEmpty(err)) this.$post(this.serverApi.@(Model.EntityName).save, this.formOption.data).then(res => this.$formSubmitAfter(this, res));
            else this.$fromValidErrorMsg();
        },
        async close() { }
    }
};
</script>

<style></style>
